package com.wyhello.tube4.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wyhello.tube4.dao.MaintenanceRecordsDAO;
import com.wyhello.tube4.pojo.Macroscopic;
import com.wyhello.tube4.pojo.Reality;
/**
 * 维护记录信息
 * @author Administrator
 *
 */
public class MaintenanceRecordsDAOImpl  implements MaintenanceRecordsDAO{
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/**
	 * 宏观测量信息的录入
	 * @param macroscopic 
	 * @return
	 * 信息录入是否成功
	 */
	public boolean saveMacroscopic(Macroscopic macroscopic){
			int i = 1;
			Session session  =  sessionFactory.getCurrentSession();
			try{
				session.save(macroscopic);
			}catch(Exception e){
				i=0;
			}
			if(i==1){
				return true;
			}else{
				return false;
			}
	}

	/**
	 * 批量删除信息
	 * 返回删除的条数
	 */
	public int deleteMacroscopic(String ids){
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "delete from macroscopic where id in "+ids;
		int i = session.createSQLQuery(sql).executeUpdate();
		return i;
	}
	/**
	 * 查询全部数据信息
	 * @return 全部数据的结果
	 */
	public List<Macroscopic> queryMacroscopic(int offset){
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Macroscopic order by time desc";
		Query query = session.createQuery(hql);
		query.setFirstResult(offset);
		query.setMaxResults(10);
		List<Macroscopic> list = query.list();
		return list;
	}
	public List<Macroscopic> queryMacTime(int offset,String begin,String end){
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Macroscopic where time between '"+begin+"' AND '"+end+"' order by time desc";
		Query query = session.createQuery(hql);
		query.setFirstResult(offset);
		query.setMaxResults(10);
		List<Macroscopic> list = query.list();
		return list;
	}
	/**
	 * 根据ID查询一条信息的数据
	 * @param id 传入要查询的id
	 * @return 返回该ID的结果
	 */
	public Macroscopic  getMacroscopicById(int id){
		Session session  =  sessionFactory.getCurrentSession();
		Macroscopic macroscopic = (Macroscopic) session.get(Macroscopic.class, Integer.valueOf(id));
		return macroscopic;
	}
	/**
	 * 根据传入的对象进行修改
	 * @param macroscopic 修改后的数据
	 * @return 是否修改成功
	 */
	public boolean updateMacroscopic(Macroscopic macroscopic){
		Session session  =  sessionFactory.getCurrentSession();
		try{
			session.update(macroscopic);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	/**
	 * 查询记录数量
	 * @return
	 */
	public int queryCountMacroscopic(){
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "select count(*) from macroscopic";
		BigInteger bi  = (BigInteger) session.createSQLQuery(sql).list().get(0);
		int count = bi.intValue();
		return count;
	}
	public int queryCountMacTime(String begin,String end){
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "select count(*) from macroscopic where time between '"+begin+"' AND '"+end+"'";
		BigInteger bi  = (BigInteger) session.createSQLQuery(sql).list().get(0);
		int count = bi.intValue();
		return count;
	}
	@Override
	public List<Macroscopic> queryMacroscopicTime(int positionId, String begin,
			String end) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "from Macroscopic where time between '"+begin+"' AND '"+end+"' AND positionId="+positionId+" order by time desc";
		List<Macroscopic>  macroscopics = session.createQuery(sql).list();
		return macroscopics;
	}
	
	
	/**
	 * 劣化分析-饼图-管排
	 * @param realField 原始值
	 * @param warnField 预警值
	 * @param rowId 管排id
	 * @return
	 */
	@Override
	public List getMacroscopicPieRow(String field, int rowId){
		
		String sql = "SELECT COUNT(a.id)  FROM macroscopic a, POSITION b,tube c " +
				" WHERE a." + field + " = 1" +
				" AND a.positionId=b.id" +
				" AND b.tubeId=c.id" +
				" AND c.resideId =" + rowId + 
				" UNION ALL" +
				" SELECT COUNT(b.id) FROM POSITION b,tube c" +
				" WHERE b.tubeId=c.id" +
				" AND c.resideId =" + rowId;
		
		//System.out.println(sql);
		Session session  =  sessionFactory.getCurrentSession();
		
		List list = session.createSQLQuery(sql).list();
		
		return list;
		
	}
	
	/**
	 * 劣化分析-饼图-受热面
	 * @param realField 原始值
	 * @param warnField 预警值
	 * @param rowId 受热面id
	 * @return
	 */
	@Override
	public List getMacroscopicPieSurface(String field, int surfaceId){
		
		String sql = "SELECT COUNT(a.id) AS '超标位置数/受热面位置总数' FROM macroscopic a, POSITION b,tube c, reside d " +
				" WHERE a." + field + " = 1" +
				" AND a.positionId=b.id" +
				" AND b.tubeId=c.id" +
				" AND c.resideId =d.id" +
				" AND d.parentId =" + surfaceId + 
				" UNION ALL" +
				" SELECT COUNT(b.id) FROM POSITION b,tube c, reside d " +
				" WHERE b.tubeId=c.id" +
				" AND c.resideId =d.id" + 
				" AND d.parentId =" + surfaceId;
		
		//System.out.println(sql);
		Session session  =  sessionFactory.getCurrentSession();
		
		List list = session.createSQLQuery(sql).list();
		
		return list;
		
	}
	
	
	/**
	 * 劣化分析-饼图-锅炉
	 * @param realField 原始值
	 * @param warnField 预警值
	 * @param rowId 锅炉id
	 * @return
	 */
	@Override
	public List getMacroscopicPieBoiler(String field, int boilerId){
		
		String sql = "SELECT COUNT(a.id) AS '超标位置数/锅炉位置总数' FROM macroscopic a, POSITION b,tube c, reside d, reside e " +
				" WHERE a." + field + " = 1" +
				" AND a.positionId=b.id" +
				" AND b.tubeId=c.id" +
				" AND c.resideId =d.id" +
				" AND d.parentId= e.id" + 
				" AND e.parentId =" + boilerId + 
				" UNION ALL" +
				" SELECT COUNT(b.id) FROM POSITION b,tube c, reside d, reside e " +
				" WHERE b.tubeId=c.id" +
				" AND c.resideId =d.id" + 
				" AND d.parentId= e.id" + 
				" AND e.parentId =" + boilerId;
		
		//System.out.println(sql);
		Session session  =  sessionFactory.getCurrentSession();
		
		List list = session.createSQLQuery(sql).list();
		
		return list;
		
	}
}
